package com.wll.exportfile.world;

import com.wll.exportfile.world.entity.UserInfo;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.images.ByteArrayImageProvider;
import fr.opensagres.xdocreport.document.images.FileImageProvider;
import fr.opensagres.xdocreport.document.images.IImageProvider;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;

import java.io.*;
import java.util.*;
import java.util.List;

public class IXDocReportExportWorld {

    public static void main(String[] args) {
        exportTemptWord();
    }


    /**
     * 基于 Freemarker模板导出world  字符 图片
     */
    public static void exportTemptWord() {

        InputStream in = null;
        FileOutputStream out = null;
        try {
            //获取本地目录的word模板
            in = new FileInputStream(new File("E://my_work//study//exportFile//src//main//resources//world//IXDocReport.docx"));
            //注册xdocreport实例并加载FreeMarker模板引擎
            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Freemarker);
            //创建xdocreport上下文对象
            IContext context = report.createContext();
            //将需要替换的数据数据添加到上下文中
            //其中key为word模板中的域名，value是需要替换的值
            //装载数据
            processData(context);
            //创建字段元数据，需要表格才加下面这两行，否则不用
            FieldsMetadata fm = report.createFieldsMetadata();
            //Word模板中的表格数据对应的集合类型
            fm.load("users", UserInfo.class, true);

            // 单个图片插入  模板插入占位图片 插入书签 picture
            IImageProvider zhaopian = new FileImageProvider(new File("C://Users//Administrator//Pictures//112.png"),true);
            zhaopian.setSize(100F, 100F);
            fm.addFieldAsImage("picture");
            report.setFieldsMetadata(fm);
            context.put("picture", zhaopian);


            //输出到本地目录
            out = new FileOutputStream(new File("E://temp//" + System.currentTimeMillis() + ".docx"));
            //处理word文档并输出
            report.process(context, out);

//            one.close();
//            two.close();
//            three.close();

        } catch (IOException e) {
            System.out.println("读取Word模板异常");
            System.out.println(e.getMessage());
        } catch (XDocReportException e) {
            System.out.println("读取Word模板异常");
            System.out.println(e.getMessage());
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void processData(IContext context) throws IOException {
        context.put("title", "测试模板导出标题");
        context.put("author", "敬亭山");
        context.put("createTime", "2022年8月19日");
        context.put("content", "申请开始测试模板导出标题");
        context.put("sign", "敬亭山.聚义");
        context.put("companyName", "北京道亨技术有限公司");
        List<UserInfo> users = new ArrayList<UserInfo>();

        //图片这里放图片的输入流
        InputStream p1 = new FileInputStream(new File("C://Users//Administrator//Pictures//2.png"));
        InputStream p2 = new FileInputStream(new File("C://Users//Administrator//Pictures//2.png"));

        users.add(new UserInfo(1, 10, "你好", "上海", "13231", "男", "KONO DIO DA", "描述", new ByteArrayImageProvider(p2)));
        users.add(new UserInfo(2, 18, "张三", "北京", "14564", "女", "12957451", "描述", new ByteArrayImageProvider(p2)));
        users.add(new UserInfo(3, 20, "李四", "武汉", "15444", "男", "个爱搜纪委", "描述", new ByteArrayImageProvider(p2)));
        users.add(new UserInfo(4, 25, "王五", "广州", "13213", "女", "没出息捏", "描述", new ByteArrayImageProvider(p1)));
        users.add(new UserInfo(5, 25, "赵六", "深圳", "154564", "男", "群殴IU为居民", "描述", new ByteArrayImageProvider(p1)));
        users.add(new UserInfo(6, 22, "钱七", "杭州", "145645", "男", "IU前往合肥南", "描述", new ByteArrayImageProvider(p1)));
//        for (int i = 7; i < 1007; i++) {
//            users.add(new UserInfo(i, RandomUtil.randomInt(10, 100), HanziUtils.getRandomHanZi(2, 4), HanziUtils.getRandomHanZi(5, 10), "14564513245689", "男", HanziUtils.getRandomHanZi(10, 20), HanziUtils.getRandomHanZi(10, 20), new ByteArrayImageProvider(p2)));
//        }
        context.put("users", users);

//        List<String> picList = new ArrayList<>();
//        picList.add("C://Users//Administrator//Pictures//1.png");
//        picList.add("C://Users//Administrator//Pictures//2.png");
//        picList.add("C://Users//Administrator//Pictures//3.png");
//        context.put("picList", picList);

    }
}
